//
//  LFBaseViewModel.m
//  AntMarket
//
//  Created by LF on 2017/3/17.
//  Copyright © 2017年 LF. All rights reserved.
//

#import "LFBaseViewModel.h"

@implementation NetModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.page = 1;
    }
    return self;
}

- (void)setType:(RefreshType)type {
    
    if (type == RefreshTypeUp) {
        self.page ++;
    } else {
        self.page = 1;
    }
    
    NSMutableDictionary *para = [NSMutableDictionary dictionaryWithDictionary:self.para];
    [para setObject:@(self.page) forKey:@"page"];
    
    self.para = para;
}

@end

@implementation LFBaseViewModel

- (LFNetworkResult *) lf_setValuesForKeysWithJSON:(id)json
{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableContainers error:nil];
    
    return [LFNetworkResult mj_objectWithKeyValues:dic];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    LFBaseViewModel *viewModel = [super allocWithZone:zone];
    [viewModel lf_initialize];
    return viewModel;
}

- (void)lf_initialize {};
- (void) clarifyResult:(LFNetworkResult *)result withStatus1:(void(^)())success status0:(void(^)())failed{

    result.isFromVM = YES;
    
    [result clarifyResultWithStatus1:^{
        if (success) {
            success();
        }
    } status0:^{
        if (failed) {
            failed();
        }
    } otherCallBack:^(NSInteger code) {
        if (code == 10) {
            
        }
//        otherCallBack(code);
    }];
}


/**
 获取分页数据

 @return racommand
 */
- (RACCommand *)getList {
    if (!_getList) {
        
        _getList = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                [LFNetworkTool httpRequest:0 url:API_APPEND_(self.net.api) para:self.net.para success:^(id json) {
                    
                    // 请求成功
                    if ([json[@"status"] intValue] == 1) {
                        // 获取到数据
                        
                        // 移除第一页数据
                        if (self.net.page == 1) {
                            [self.datas removeAllObjects];
                        }
                        
                        // 获取模型类
                        Class ModelClass = NSClassFromString(self.net.model_name);
                        // 本次模型数组
                        NSArray *modelArr = [ModelClass mj_objectArrayWithKeyValuesArray:json[@"datas"]];
                        // 插入数据
                        [self.datas addObjectsFromArray:modelArr];
                        
                        // 是否加载完毕
                        [subscriber sendNext:@(self.datas.count == [json[@"total"] intValue])];
                        [subscriber sendCompleted];
                    } else {
                        // 接口报错
                        [subscriber sendError:MYError(@"12")];
                        
                        // 报错,把多加的页数还原回去
                        if (self.net.page > 1) {
                            self.net.page--;
                        }
                    }
                    
                } failure:^(NSError *error) {
                    
                    // 报错,把多加的页数还原回去
                    if (self.net.page > 1) {
                        self.net.page--;
                    }
                    
                    // 网络连接错误
                    [subscriber sendError:error];
                    
                }];
                return nil;
            }];
        }];
    }
    return _getList;
}

- (NetModel *)net {
    if (!_net) {
        _net = [[NetModel alloc] init];
    }
    return _net;
}

- (NSMutableArray *)datas {
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (RACSubject *)errorSubject {
    if (!_errorSubject) {
        _errorSubject = [RACSubject subject];
    }
    return _errorSubject;
}

-  (RACSubject *)refreshUISubject {
    if (!_refreshUISubject) {
        _refreshUISubject = [RACSubject subject];
    }
    return _refreshUISubject;
}

- (RACSubject *)refreshEndSubject {
    if (!_refreshEndSubject) {
        _refreshEndSubject = [RACSubject subject];
    }
    return _refreshEndSubject;
}

- (RACSubject *)loadDatasSubject {
    if (!_loadDatasSubject) {
        _loadDatasSubject = [RACSubject subject];
    }
    return _loadDatasSubject;
}

@end
